var e=`0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`,t=`A00000000000000000000000000`;function n(e){if(e>=`a`&&e<=`z`)return e.charCodeAt(0)-97+2;if(e>=`A`&&e<=`Z`)return 90-e.charCodeAt(0)+2;throw Error(`invalid order key head: ${e}`)}function r(e){let t=n(e[0]);if(t>e.length)throw Error(`invalid order key: ${e}`);return e.slice(0,t)}function i(e){if(e.length!==n(e[0]))throw Error(`invalid integer part of order key: ${e}`)}function a(e,n){if(e===t)throw Error(`invalid order key: ${e}`);let i=r(e),a=e.slice(i.length);if(a.length>0&&a[a.length-1]===n[0])throw Error(`invalid order key: ${e}`)}function o(e,t){i(e);let[n,...r]=e.split(``),a=!0;for(let e=r.length-1;a&&e>=0;e--){let n=t.indexOf(r[e])+1;n===t.length?r[e]=t[0]:(r[e]=t[n],a=!1)}if(a){if(n===`Z`)return`a${t[0]}`;if(n===`z`)return null;let e=String.fromCharCode(n.charCodeAt(0)+1);return e>`a`?r.push(t[0]):r.shift(),e+r.join(``)}return n+r.join(``)}function s(e,t){i(e);let[n,...r]=e.split(``),a=!0;for(let e=r.length-1;a&&e>=0;e--){let n=t.indexOf(r[e])-1;n===-1?r[e]=t[t.length-1]:(r[e]=t[n],a=!1)}if(a){if(n===`a`)return`Z${t[t.length-1]}`;if(n===`A`)return null;let e=String.fromCharCode(n.charCodeAt(0)-1);return e<`Z`?r.push(t[t.length-1]):r.shift(),e+r.join(``)}return n+r.join(``)}function c(e,t,n){let r=n[0];if(t!==null&&e>=t)throw Error(`${e} >= ${t}`);if(e.slice(-1)===r||t&&t.slice(-1)===r)throw Error(`trailing zero`);if(t!==null){let i=0;for(;(e[i]||r)===t[i]&&i<t.length;)i++;if(i>0)return t.slice(0,i)+c(e.slice(i),t.slice(i),n)}let i=e===``?0:n.indexOf(e[0]),a=t===null?n.length:n.indexOf(t[0]);return a-i>1?n[Math.round(.5*(i+a))]:t!==null&&t.length>1?t.slice(0,1):n[i]+c(e.slice(1),null,n)}function l(n,i,l={}){let u=l.digits??e;if(n!==null&&a(n,u),i!==null&&a(i,u),n!==null&&i!==null&&n>=i)throw Error(`${n} >= ${i}`);if(n===null){if(i===null)return`a0`;let e=r(i),n=i.slice(e.length);if(e===t)return e+c(``,n,u);if(e<i)return e;let a=s(e,u);if(a===null)throw Error(`cannot decrement any more`);return a}if(i===null){let e=r(n),t=n.slice(e.length),i=o(e,u);return i===null?e+c(t,null,u):i}let d=r(n),f=n.slice(d.length),p=r(i),m=i.slice(p.length);if(d===p)return d+c(f,m,u);let h=o(d,u);if(h===null)throw Error(`cannot increment any more`);return h<i?h:d+c(f,null,u)}function u(e,t,n){let r=``;for(let i=0;i<e;i++){let a=i===e-1?1+Math.floor(n()*(t.length-1)):Math.floor(n()*t.length);r+=t[a]}return r}function d(t,n,r={}){let i=r.digits??e,a=r.jitter??4,o=r.random??Math.random,s=l(t,n,{digits:i});if(a<=0)return s;for(let e=0;e<8;e++){let e=s+u(a,i,o);if(n===null||e<n)return e}return s}export{d as t};